package com.example.goodpet.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.goodpet.R;
import com.example.goodpet.utils.ActivityManager;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.zackratos.ultimatebarx.ultimatebarx.java.UltimateBarX;

public class WebViewActivity extends FragmentActivity {
    String TAG=this.getClass().getSimpleName();
    private TextView cmTitleText;
    private ImageView cmTitleLeft;
    private TextView cmTitleRight;
    private ProgressBar progressBar;
    private WebView webview;
    private String url;
    private boolean showTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        UltimateBarX.statusBarOnly(this)
                .fitWindow(true)
                .colorRes(R.color.ic_back_background)
                .light(true)
                .apply();
        ActivityManager.getInstance().addActivity(this);

        initIntent(getIntent());
        initView();
        initListener();
        initData();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ActivityManager.getInstance().removeCurrent();
    }

    public static void actionStart(Context context, String url) {
            Intent intent = new Intent(context, WebViewActivity.class);
            intent.putExtra("url", url);
            context.startActivity(intent);
    }

    public static void actionStart(Context context, String url, boolean showTitle) {
            Intent intent = new Intent(context, WebViewActivity.class);
            intent.putExtra("url", url);
            intent.putExtra("showTitle", showTitle);
            context.startActivity(intent);
    }


    protected void initIntent(Intent intent) {
            url = intent.getStringExtra("url");
            showTitle = intent.getBooleanExtra("showTitle", true);
    }


        protected void initView() {
            cmTitleLeft=findViewById(R.id.cmTitleLeft);
            cmTitleText=findViewById(R.id.cmTitleText);
            cmTitleRight=findViewById(R.id.cmTitleRight);
            webview = findViewById(R.id.webview);
            progressBar = findViewById(R.id.progress_bar);

            cmTitleRight.setVisibility(View.INVISIBLE);
            if(!showTitle) {
                cmTitleText.setVisibility(View.GONE);
            }
        }


        protected void initListener() {

            cmTitleLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(webview.canGoBack()) {
                        webview.goBack();
                    }else {
                        onBackPressed();
                    }
                }
            });
        }

        @Override
        protected void onResume() {
            super.onResume();
            webview.onResume();
            Log.d(TAG, "onResume: "+this.getTaskId());
        }

        @Override
        protected void onPause() {
            super.onPause();
            webview.onPause();
        }


        protected void initData() {

            if(!TextUtils.isEmpty(url)) {
                webview.loadUrl(url);
            }
            //配置WebSettings
            WebSettings settings = webview.getSettings();
            //设置自适应屏幕，两者合用
            settings.setUseWideViewPort(true); //将图片调整到适合webview的大小
            settings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
            //缩放操作
            settings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
            settings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
            settings.setDisplayZoomControls(false); //隐藏原生的缩放控件
            //其他操作
            settings.setLoadsImagesAutomatically(true); //支持自动加载图片

            //配置WebViewClient，使用WebView加载
            webview.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }

                @Override
                public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                    handler.proceed();//表示等待证书相应
                }
            });

            //配置WebChromeClient类
            webview.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onReceivedTitle(WebView view, String title) {
                    cmTitleText.setText(title);
                }

                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    if(newProgress < 100) {
                        progressBar.setVisibility(View.VISIBLE);
                        progressBar.setProgress(newProgress);
                    }else {
                        progressBar.setVisibility(View.INVISIBLE);
                    }
                }
            });
        }



}